package graph;

import org.junit.Test;

/*
 *@description
 *@author hkfan
 *@version 1.0
 *@create 2025/5/19 09:55
 */
public class LeetCode_200 {

    @Test
    public void test(){
        char[][] grid = new char[][]{
                {'1','1'},
                {'1','0'}
        };
        System.out.println(numIslands(grid));
    }


    public int numIslands(char[][] grid) {
        //1. 定义变量
        int[][] visited = new int[grid.length][grid[0].length];
        int res = 0;
        //2. 循环遍历
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length;j++) {
                if (visited[i][j]==0 && grid[i][j]=='1'){
                    visit(grid,visited,i,j);
                    res++;
                }
            }
        }
        return res;

    }

    private void visit(char[][] grid, int[][] visited, int i, int j) {
        if (!(i>=0 && i<=grid.length-1) || !(j>=0 && j<=grid[0].length-1) || visited[i][j]==1 || grid[i][j]=='0'  ){
            return;
        }
        visited[i][j]=1;
        visit(grid,visited,i+1,j);
        visit(grid,visited,i,j+1);
        visit(grid,visited,i-1,j);
        visit(grid,visited,i,j-1);
    }


}
